package com.rabbit.videoplayer.widget

import android.app.Activity
import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.FrameLayout
import androidx.constraintlayout.widget.ConstraintLayout
import com.rabbit.videoplayer.listener.OnVideoStatusListener
import com.rabbit.videoplayer.R
import com.rabbit.videoplayer.listener.OnAdReadyLoadListener
import com.rabbit.videoplayer.listener.OnLoadUnlockAdListener


class AdView(context: Context, attrs: AttributeSet? = null) : ConstraintLayout(context, attrs), OnVideoStatusListener {

    private val pauseAdRoot: FrameLayout
    // 解锁uiRoot
    private val layoutUnlockAdRoot: View
    // 解锁返回
    private val btnUnLockBack: View
    // 加载解锁广告
    private val btnUnLockLoad: View
    // 暂停广告
//    private var pauseAdTools: CommonAdTools? = null
    private var listener: OnLoadUnlockAdListener? = null
    private var adListener: OnAdReadyLoadListener? = null
    // 是否已关闭暂停广告
    private var isClosePauseAd = false

    init {
        LayoutInflater.from(context).inflate(R.layout.view_wb_mix_vod_ad, this, true)
        pauseAdRoot = findViewById(R.id.pauseAdRoot)
        layoutUnlockAdRoot = findViewById(R.id.layout_unlock_ad)
        btnUnLockBack = findViewById(R.id.view_lock_back_ic)
        btnUnLockLoad = findViewById(R.id.layoutUnLock)

        btnUnLockBack.setOnClickListener {
            if (context is Activity) {
                context.finish()
            }
        }

        btnUnLockLoad.setOnClickListener {
            listener?.onLoadUnlockAd()
        }

        hide()
    }


    private fun hide() {
        if (this.visibility != View.GONE) {
            this.visibility = View.GONE
        }
        hideUnlockUI()

        clearPauseAd()
    }


    private fun show() {
        if (this.visibility != View.VISIBLE) {
            this.visibility = View.VISIBLE
        }
    }

    /**
     * 根据横屏或者竖屏，缩放暂停广告
     * @param isSmall 缩放
     */
    private fun scalePauseAd(isSmall: Boolean) {
        if (isSmall) {
            pauseAdRoot.animate().scaleX(1.0f).scaleY(1.0f).start()
        } else {
            pauseAdRoot.animate().scaleX(1.5f).scaleY(1.5f).start()
        }
    }

    /** 关闭暂停广告 */
    private fun clearPauseAd() {
        pauseAdRoot.removeAllViews()
        pauseAdRoot.visibility = View.GONE
//        pauseAdTools?.release()
    }

    private fun showUnlockUI(){
        if (layoutUnlockAdRoot.visibility != View.VISIBLE) {
            layoutUnlockAdRoot.visibility = View.VISIBLE
        }
    }
    private fun hideUnlockUI(){
        if (layoutUnlockAdRoot.visibility != View.GONE) {
            layoutUnlockAdRoot.visibility = View.GONE
        }
    }

    private fun release(){
        clearPauseAd()
        hideUnlockUI()
        hide()
    }


    fun setLoadUnlockAdListener(listener: OnLoadUnlockAdListener){
        this.listener = listener
    }

    fun setReady2AdListener(listener: OnAdReadyLoadListener){
        this.adListener = listener
    }

    /**
     * 加载暂停广告
     * @param isDisplayLoadAd 显示或隐藏广告
     */
    fun loadPauseAd(isDisplayLoadAd: Boolean) {
        hideUnlockUI()
        if (context == null || context !is Activity) return
        val activity: Activity = context as Activity
        if (activity.isFinishing || activity.isDestroyed) return
        // 设置关闭广告的标签
        isClosePauseAd = !isDisplayLoadAd
        if (isClosePauseAd) { // 关闭广告
            // 隐藏当前的广告Root
            release()
            return
        }
        adListener?.onReady2LoadAd()
//        // 准备展示广告
//        if (pauseAdTools == null) {
//            pauseAdTools = CommonAdTools()
//        }
//        pauseAdTools?.loadAd(activity, AdConstants.AD_ID_VIDEO_DETAIL_PAUSE, object : OnLoadAdCallback() {
//            override fun onAdLoadFailed(errorCode: Int) {
//                super.onAdLoadFailed(errorCode)
//                release()
//            }
//
//            override fun onAdLoaded(ad: Any?, vararg tag: Int) {
//                super.onAdLoaded(ad, *tag)
//                // 如果用户已经关闭了广告
//                if (isClosePauseAd) {
//                    release()
//                } else {
//                    // 广告放到PauseAdRoot中
//                    pauseAdTools?.showAd(pauseAdRoot)
//                    show()
//                }
//            }
//        })
    }

    fun onAdLoadFailed() {
        release()
    }

    fun onAdLoaded(): ViewGroup? {
        // 如果用户已经关闭了广告
        return if (isClosePauseAd) {
            release()
            null
        } else {
            show()
            pauseAdRoot
        }
    }

    override fun onDetachedFromWindow() {
        super.onDetachedFromWindow()
        release()
    }

    override fun onVodInit(vararg init: Any?): Boolean {
        return false
    }

    override fun onVodPrepared(vararg prepared: Any) {

    }

    override fun onVodStart() {
        loadPauseAd(false)
    }

    override fun onVodFirstFrameStart() {

    }

    override fun onVodSwitchDisplayControlView() {

    }

    override fun onVodPause() {
    }

    override fun onVodStop() {

    }

    override fun onVodError(vararg params: Any) {

    }

    override fun onVodComplete() {

    }

    override fun onVodRelease() {
        release()
    }

    override fun onVodLockedScreen(vararg params: Any) {

    }

    override fun onVodFullScreen(vararg params: Any) {
        if (params.isEmpty()) return
        val isFullScreen = params[0] as Boolean
        scalePauseAd(!isFullScreen)
    }

    override fun onVodLoadingBegin() {
    }

    override fun onVodLoadingProgress(vararg params: Any) {

    }

    override fun onVodLoadingEnd() {
    }

    override fun onVodBackPress(): Boolean {
        return false
    }

    override fun onEpisodeSelected() {
    }

    override fun onVodShowUnlockUI(isDisplayUI: Boolean) {
        if (isDisplayUI) {
            show()
            clearPauseAd()
            showUnlockUI()
        } else {
            release()
        }
    }

    override fun onVodSkits() {

    }


}